<script lang="ts" setup>
import { get, post, message, loadFile } from '@/utils'
import { FormInstance } from '@arco-design/web-vue'
const props = defineProps<{
  show: boolean
  id: number | string
}>()
const emit = defineEmits(['update:show', 'reset', 'confirm'])
const formRef = ref() as Ref<FormInstance>
const detail = reactive({
  id: props.id || '',
  direct: 0,
  indirect: 0,
  level: '',
  name: '',
})
const submit = done => {
  formRef.value.validate(err => {
    if (!err) {
      const query = Object.assign({}, detail)
      const url = props.id ? '/server-shop/admin/place/level/update' : '/server-shop/admin/place/level/save'
      post(
        { url, data: query },
        ({ code, msg }) => {
          message({
            type: code == 200 ? 'success' : 'error',
            content: msg,
          })
          if (code == 200) {
            emit('update:show', false)
            emit('confirm')
          }
          done(code == 200)
        },
        () => done(false)
      )
    } else done(false)
  })
}
const getInfo = () => {
  get({ url: '/server-shop/admin/place/level/info', data: { id: props.id } }, ({ data }) => {
    Object.assign(detail, data)
  })
}
onMounted(() => {
  if (props.id) getInfo()
})
const options = ref<any[]>([])
for (let i = 1; i < 9; i++) {
  options.value.push({
    label: i + '',
    value: i,
  })
}
</script>
<template>
  <CDrawer v-model:visible="props.show" @before-ok="submit" :mask-closable="false" width="500px" :title="props.id ? '编辑' : '新建'" @cancel="emit('update:show', false)">
    <a-form :model="detail" ref="formRef" :label-col-props="{ span: 6 }" :wrapper-col-props="{ span: 16 }">
      <a-form-item label="等级名称" field="name" :rules="[{ required: true, message: '请输入等级名称' }]" :validate-trigger="['change', 'blur']">
        <a-input v-model="detail.name" placeholder="请输入等级名称" />
      </a-form-item>
      <a-form-item label="等级" field="level" :rules="[{ required: true, message: '请选择等级' }]" :validate-trigger="['change', 'blur']">
        <a-select :options="options" v-model="detail.level" placeholder="请选择等级" />
      </a-form-item>
      <a-form-item label="直推比例" field="direct" :rules="[{ required: true, message: '请输入直推比例' }]" :validate-trigger="['change', 'blur']">
        <a-input-number :min="0" v-model="detail.direct" placeholder="请输入直推比例">
          <template #append>%</template>
        </a-input-number>
      </a-form-item>
      <a-form-item label="间接比例" field="indirect" :rules="[{ required: true, message: '请输入间接比例' }]" :validate-trigger="['change', 'blur']">
        <a-input-number :min="0" v-model="detail.indirect" placeholder="请输入间接比例">
          <template #append>%</template>
        </a-input-number>
      </a-form-item>
    </a-form>
  </CDrawer>
</template>
<style lang="less" scoped></style>
